package com.standbyme.whitelistspringbootstarter.aop;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.standbyme.whitelistspringbootstarter.annotation.DoWhiteList;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Objects;

@Aspect
@Component
public class DoJoinPoint {

    @Autowired
    private String whiteListConfig;

    private Logger logger = LoggerFactory.getLogger(DoJoinPoint.class);

    @Pointcut("@annotation(com.standbyme.whitelistspringbootstarter.annotation.DoWhiteList)")
    public void addPoint() {

    }

    @Around(value = "addPoint()")
    public Object aroud(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取请求调用的控制层方法对象
        Method moehod = getMoehod(joinPoint);
        //获取注解上配置的值
        DoWhiteList annotation = moehod.getAnnotation(DoWhiteList.class);
        String value = annotation.value();

        //在传参中查询是否存在配置参数
        String fileValue = getFileValue(value, joinPoint.getArgs());
        //如果传参中不存在该参数
        if (Objects.isNull(fileValue) || "".equals(fileValue)) {
            return joinPoint.proceed();
        }
        //判断参数是否在配置中
        String[] targets = whiteListConfig.split(",");
        for (String target : targets) {
            if (target.equals(fileValue)) {
                return joinPoint.proceed();
            }
        }

        return getReturnJson(moehod, annotation);

    }

    private Method getMoehod(JoinPoint joinPoint) throws NoSuchMethodException {
        MethodSignature signature= (MethodSignature)joinPoint.getSignature();
        return joinPoint.getTarget().getClass().getMethod(signature.getName(),signature.getParameterTypes());
    }


    public String getFileValue(String filed,Object[] args) {

        String fileValue = null;

        for (Object arg : args) {
            try {
                if (fileValue == null || "".equals(fileValue)) {
                    fileValue = BeanUtils.getProperty(arg, filed);
                } else {
                    break;
                }
            } catch (Exception e) {
                if (args.length == 1) {
                    return args[0].toString();
                }
            }

        }
        return fileValue;
    }

    private Object getReturnJson(Method method,DoWhiteList annotation) throws IllegalAccessException, InstantiationException, JsonProcessingException {
        Class<?> returnType = method.getReturnType();
        if ("".equals(annotation.returnJson())) {
            return returnType.newInstance();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(annotation.returnJson());
    }
}
